/*
Copyright (C) 2021-2025 Casa Xu (also Zhiyan Xu) from HIT

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*
 * LOS_FotonAmur.c
 *
 *  Created on: 2023年4月16日
 *      Author: 64435
 */

#include "LOS_FotonAmur.h"

uint8_t Pack2Amur[128];

void LOS_FotonAmur_check()
{
    uint8_t data[12]={0x07, 0x10, 0x08, 0x06, 0x07, 0xD2, 0xFF, 0x01, 0x01, 0x01, 0x01, 0x00};
    LOS_MAX14830_ResetFIFO(FotonAmur_MAXNUM, FotonAmur_MAXPort);
    LOS_MAX14830_WriteBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, data ,12);
    STA_FotonAmur.HL_TXF = 1;
}

void LOS_FotonAmur_SendCommand(FotonAmurCMD_t cmd) 
{
    static uint8_t origin_cmd[50]; //COBS编码前原始指令,预留50字节空间
    static uint8_t final_cmd[52]; //COBS编码后指令,预留52字节空间
    uint16 crc16 = 0;
    uint8_t header[10]={0x10, 0x08, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    //CRC16计算
    crc16 = LOS_FP_CRC16Cal(cmd.data, 0, cmd.length);
    memcpy(origin_cmd, &crc16, 2);
    LOS_FP_BLEndianTransfer(origin_cmd, 2); //转小端
    memcpy(&header[8], origin_cmd, 2);
    //数据长度
    memcpy(origin_cmd, &cmd.length, 2);  
    LOS_FP_BLEndianTransfer(origin_cmd, 2); //转小端
    memcpy(&header[6], origin_cmd, 2);
    //消息id，已经填成小端格式了，不用再弄了
    header[4] = cmd.msgid[0];
    header[5] = cmd.msgid[1];
    //组帧
    memcpy(origin_cmd, header, 10);
    memcpy(&origin_cmd[10], cmd.data, cmd.length);
    //COBS编码
    LOS_FP_CobsEncode(origin_cmd, 10 + cmd.length, final_cmd);
    final_cmd[11 + cmd.length] = 0x00;
    LOS_MAX14830_ResetFIFO(FotonAmur_MAXNUM, FotonAmur_MAXPort);
    LOS_MAX14830_WriteBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, final_cmd ,12 + cmd.length);
    STA_FotonAmur.HL_TXF = 1;
}

void LOS_FotonAmur_SetTime()
{
    static uint8_t year = 0;
    year = LOS_RealTime_Year - 2000;
    AmurCMD_Time.data[0] = LOS_RealTime_Month;
    AmurCMD_Time.data[1] = LOS_RealTime_Day;
    AmurCMD_Time.data[2] = year;
    AmurCMD_Time.data[3] = LOS_RealTime_Hour;
    AmurCMD_Time.data[4] = LOS_RealTime_Minute;
    AmurCMD_Time.data[5] = LOS_RealTime_Second;
    LOS_FotonAmur_SendCommand(AmurCMD_Time);
}

void LOS_FotonAmur_ClearBuf()
{
    static uint8_t FotonAmur_rstbuf = 0x00;
    LOS_MAX14830_ResetFIFO(FotonAmur_MAXNUM, FotonAmur_MAXPort);
    LOS_MAX14830_WriteBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, &FotonAmur_rstbuf ,1);
}

void LOS_FotonAmur_Shutdown()
{
    static uint8_t FotonAmur_ShutDown[12] = {0x07, 0x10, 0x08, 0x06, 0x07, 0XFF, 0X09, 0X01, 0X01, 0X01, 0X01, 0X00};
    for(int i = 0; i<128; i++)
    {
        Pack2Amur[i] = 0;
    }
    memcpy(&Pack2Amur[1], FotonAmur_ShutDown, 12);
    LOS_MAX14830_ResetFIFO(FotonAmur_MAXNUM, FotonAmur_MAXPort);
    LOS_MAX14830_WriteBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, Pack2Amur ,128);
    STA_FotonAmur.HL_TXF = 1;
}

void LOS_FotonAmur_ShowTime()
{
    static uint8_t FotonAmur_ShutDown[12] = {0x07, 0x10, 0x08, 0x06, 0x07, 0X31, 0X08, 0X01, 0X01, 0X01, 0X01, 0X00};
    LOS_MAX14830_ResetFIFO(FotonAmur_MAXNUM, FotonAmur_MAXPort);
    LOS_MAX14830_WriteBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, FotonAmur_ShutDown ,12);
    STA_FotonAmur.HL_TXF = 1;
}

void LOS_FotonAmur_GetTemp()
{
    static uint8_t FotonAmur_ShutDown[13] = {0x08, 0x10, 0x08, 0x06, 0x07, 0X28, 0X08, 0X01, 0X04, 0X38, 0X87, 0X3B, 0x00};
    LOS_MAX14830_ResetFIFO(FotonAmur_MAXNUM, FotonAmur_MAXPort);
    LOS_MAX14830_WriteBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, FotonAmur_ShutDown ,13);
    STA_FotonAmur.HL_TXF = 1;
}

void LOS_FotonAmur_InitStruct()
{
    AmurCMD_TEMP.length = 1;
    AmurCMD_TEMP.msgid[0] = 0x00;
    AmurCMD_TEMP.msgid[1] = 0x09;
    AmurCMD_TEMP.data[0] = 0X0C;

    AmurCMD_Q1.length = 4;
    AmurCMD_Q1.msgid[0] = 0x01;
    AmurCMD_Q1.msgid[1] = 0x09;
    AmurCMD_Q1.data[0] = 0x78;
    AmurCMD_Q1.data[1] = 0x56;
    AmurCMD_Q1.data[2] = 0x34;
    AmurCMD_Q1.data[3] = 0x12;

    AmurCMD_Q2.length = 4;
    AmurCMD_Q2.msgid[0] = 0x02;
    AmurCMD_Q2.msgid[1] = 0x09;
    AmurCMD_Q2.data[0] = 0xEF;
    AmurCMD_Q2.data[1] = 0xCD;
    AmurCMD_Q2.data[2] = 0xAB;
    AmurCMD_Q2.data[3] = 0x90;

    AmurCMD_Q3.length = 4;
    AmurCMD_Q3.msgid[0] = 0x03;
    AmurCMD_Q3.msgid[1] = 0x09;
    AmurCMD_Q3.data[0] = 0x4D;
    AmurCMD_Q3.data[1] = 0x3C;
    AmurCMD_Q3.data[2] = 0x2B;
    AmurCMD_Q3.data[3] = 0x1A;

    AmurCMD_Q4.length = 4;
    AmurCMD_Q4.msgid[0] = 0x04;
    AmurCMD_Q4.msgid[1] = 0x09;
    AmurCMD_Q4.data[0] = 0x00;
    AmurCMD_Q4.data[1] = 0x05;
    AmurCMD_Q4.data[2] = 0x00;
    AmurCMD_Q4.data[3] = 0x01;

    AmurCMD_Time.length = 6;
    AmurCMD_Time.msgid[0] = 0x30;
    AmurCMD_Time.msgid[1] = 0x08;
    AmurCMD_Time.data[0] = 0x02;
    AmurCMD_Time.data[1] = 0x01;
    AmurCMD_Time.data[2] = 0x18;
    AmurCMD_Time.data[3] = 0x0C;
    AmurCMD_Time.data[4] = 0x22;
    AmurCMD_Time.data[5] = 0x38;

    STA_FotonAmur.flag1 = 0;
    STA_FotonAmur.flag2 = 0;
    STA_FotonAmur.argprd = 60;

    for(int i = 0; i<128; i++)
    {
        Pack2Amur[i] = 0;
    }
}

void LOS_FotonAmur_PWROn()
{
    STA_FotonAmur.CMD = 1;
    STA_FotonAmur.PWRON_Time = LOS_Time_SecFromBoot;
    LOS_EPS_Switch(21, SWITCH_ON);
}

void LOS_FotonAmur_PWROff()
{
    STA_FotonAmur.CMD = 0;
    LOS_EPS_Switch(21, SWITCH_OFF);
}

void LOS_FotonAmur_TC(uint8_t* data)
{
    LOS_MAX14830_ResetFIFO(FotonAmur_MAXNUM, FotonAmur_MAXPort);
    LOS_MAX14830_WriteBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, data ,128);
    STA_FotonAmur.HL_TXF = 1;
}


uint8_t LOS_FotonAmur_PackCommand(FotonAmurCMD_t cmd, uint8_t* packet) 
{
    static uint8_t origin_cmd[50]; //COBS编码前原始指令,预留50字节空间
    static uint8_t final_cmd[52]; //COBS编码后指令,预留52字节空间
    uint16 crc16 = 0;
    uint8_t header[10]={0x10, 0x08, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
    //CRC16计算
    crc16 = LOS_FP_CRC16Cal(cmd.data, 0, cmd.length);
    memcpy(origin_cmd, &crc16, 2);
    LOS_FP_BLEndianTransfer(origin_cmd, 2); //转小端
    memcpy(&header[8], origin_cmd, 2);
    //数据长度
    memcpy(origin_cmd, &cmd.length, 2);  
    LOS_FP_BLEndianTransfer(origin_cmd, 2); //转小端
    memcpy(&header[6], origin_cmd, 2);
    //消息id，已经填成小端格式了，不用再弄了
    header[4] = cmd.msgid[0];
    header[5] = cmd.msgid[1];
    //组帧
    memcpy(origin_cmd, header, 10);
    memcpy(&origin_cmd[10], cmd.data, cmd.length);
    //COBS编码
    LOS_FP_CobsEncode(origin_cmd, 10 + cmd.length, packet);
    packet[11 + cmd.length] = 0x00;
    return (12 + cmd.length);
}

void LOS_FotonAmur_NewPack()
{
    for(int i = 0; i<128; i++)
    {
        Pack2Amur[i] = 0;
    }
    AmurCMD_Time.data[0] = LOS_RealTime_Month;
    AmurCMD_Time.data[1] = LOS_RealTime_Day;
    AmurCMD_Time.data[2] = LOS_RealTime_Year;
    AmurCMD_Time.data[3] = LOS_RealTime_Hour;
    AmurCMD_Time.data[4] = LOS_RealTime_Minute;
    AmurCMD_Time.data[5] = LOS_RealTime_Second;
    LOS_FotonAmur_PackCommand(AmurCMD_Time, &Pack2Amur[1]);
    LOS_FotonAmur_PackCommand(AmurCMD_TEMP, &Pack2Amur[19]);
    memcpy(AmurCMD_Q1.data, &STA_SSY.q_o[0], 4);
    AmurCMD_Q1.length = 4;
    LOS_FotonAmur_PackCommand(AmurCMD_Q1, &Pack2Amur[32]);
    memcpy(AmurCMD_Q2.data, &STA_SSY.q_o[1], 4);
    AmurCMD_Q2.length = 4;
    LOS_FotonAmur_PackCommand(AmurCMD_Q2, &Pack2Amur[48]);
    memcpy(AmurCMD_Q3.data, &STA_SSY.q_o[2], 4);
    AmurCMD_Q3.length = 4;
    LOS_FotonAmur_PackCommand(AmurCMD_Q3, &Pack2Amur[64]);
    memcpy(AmurCMD_Q4.data, &STA_SSY.q_o[3], 4);
    AmurCMD_Q4.length = 4;
    LOS_FotonAmur_PackCommand(AmurCMD_Q4, &Pack2Amur[80]);
    LOS_MAX14830_ResetFIFO(FotonAmur_MAXNUM, FotonAmur_MAXPort);
    LOS_MAX14830_WriteBytes(FotonAmur_MAXNUM, FotonAmur_MAXPort, Pack2Amur,128);
    STA_FotonAmur.HL_TXF = 1;
}
